﻿package Interface
{
	import com.junkbyte.console.Cc;
	
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.events.Event;	
	import flash.net.URLRequest;	
	import flash.system.Security;	
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;	
	
	public class AvatarLoader extends Sprite
	{
		protected var FLoader:Loader;
		protected var FPath:String;
		protected var FLaodingTimer:Timer;
		protected var FWidth:Number;
		protected var FHeight:Number;
		protected var FPercentage:TextField;
		//Loading utilites(avoding unknown VK bug with incomplete photo loading)
		protected var FIsPhotoLoaded:Boolean;
		protected var FIsPhotoLoading:Boolean;	
		protected var FLoadingTimer:Timer;
		protected var FPhoto:Bitmap;
		
		public function AvatarLoader():void
		{
			FLoader = new Loader();
			FLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnAvatarLoaded);
			FLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, OnAvatarLoading);
			FPercentage = getChildByName('Percentage') as TextField;
			FWidth = new Number(width);
			FHeight = new Number(height);
			
			FIsPhotoLoaded = false;
			FIsPhotoLoading = false;
			FLoadingTimer = new Timer(1000);
			FLoadingTimer.addEventListener(TimerEvent.TIMER, OnLoadingTimer);
			FPhoto = null;
		}
		
		public function set Photo(APath:String):void
		{
			FIsPhotoLoaded = false;
			FIsPhotoLoading = false;
			FPath = APath;
			UpdateSecurity(FPath);			
			FLoadingTimer.start();
		}	
		
		protected function OnLoadingTimer(e:TimerEvent):void
		{	
			if (!FIsPhotoLoading && !FIsPhotoLoaded)
			{
				FLoader.load(new URLRequest(FPath));
			}
		}
		
		protected function UpdateSecurity(APath:String):void
		{			
			Cc.logch('Avatar', 'New image: ' + APath);
			var vHost:String = APath.substr(7, APath.length).split('.')[0];
			Cc.logch('Avatar', 'Crossdomain detected: ' +  "http://" + vHost + ".userapi.com/crossdomain.xml");
			Security.loadPolicyFile("http://" + vHost + ".vk.com/crossdomain.xml");
			Security.loadPolicyFile("http://" + vHost + ".userapi.com/crossdomain.xml");
		}
		
		protected function OnAvatarLoading(e:ProgressEvent):void
		{
			FIsPhotoLoading = true;
			FPercentage.text = Math.round(e.bytesLoaded / e.bytesTotal) * 100 + ' %';
		}
		
		protected function OnAvatarLoaded(e:Event):void
		{
			FIsPhotoLoaded = true;
			FIsPhotoLoading = false;			
			FLoadingTimer.stop();
			
			if(FLoader.contentLoaderInfo.bytesLoaded == FLoader.contentLoaderInfo.bytesTotal)
			{
				if(FPhoto && FPhoto.parent == this)
				{
					removeChild(FPhoto);
					FPhoto = null;
				}
					
				FPhoto = e.target.content as Bitmap;
				FPhoto.visible = true;
				Resize(FPhoto, FWidth, FHeight);
				FPhoto.x = FWidth/2 - FPhoto.width/2;
				FPhoto.y = FHeight/2 - FPhoto.height/2;
				
				addChild(FPhoto);
				
				FIsPhotoLoaded = true;
				FIsPhotoLoading = false;
				
				dispatchEvent(new Event(Event.COMPLETE));
			}
			else
			{
				FIsPhotoLoaded = false;
				FIsPhotoLoading = false;			
				FLoadingTimer.start();
			}
		}
		
		
		protected function Resize(APhoto:Bitmap, AWidth:Number, AHeight:Number):void
		{
			var vWidth = APhoto.width;
			var vHeight = APhoto.height;
			
			if (vHeight > AHeight)
			{
				APhoto.width = vWidth * (AHeight / vHeight);            
				APhoto.height = AHeight;
			}
				
			vWidth = APhoto.width;
			vHeight = APhoto.height;
			
			if (vWidth > AWidth)
			{
				APhoto.height = vHeight * (AWidth / vWidth);            
				APhoto.width  = AWidth;
			}
		}
	}	
}
